package com.ebc.eservice.controller.pc;

import com.ebc.eservice.service.EbcArticleService;
import com.ebc.eservice.service.app.AppArticleService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.util.common.constant.ApiTextHelperConstant;
import com.ebc.eservice.util.common.constant.DeveloperConstant;
import com.ebc.eservice.vo.article.AddArticleTypeReqVo;
import com.ebc.eservice.vo.article.ArticleReqVo;
import com.ebc.eservice.vo.article.ArticleRespVo;
import com.ebc.eservice.vo.ReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: Joey
 * @Description:后台文章管理
 * @date:2022/9/25 17:25
 */

@Api(tags = "后台管理系统-[个性化内容-文章]")
@RestController
@RequestMapping("/companyExample")
public class CompanyArticleController {

    @Autowired
    private EbcArticleService ebcArticleService;
    @Autowired
    private AppArticleService appArticleService;


    @ApiOperation(value = "添加文章", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/addArticle")
    public JsonResult addArticles(@RequestBody ArticleReqVo articleReqVo){
        System.out.println("=============正在添加文章, "+articleReqVo);
        return ebcArticleService.addNewArticle(articleReqVo);
    }

    /**
     *      添加文章后 获取文章 id 调用添加照片接口
     * */
    @ApiOperation(value = "添加[修改]文章照片", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/addArticlePic")
    public JsonResult addArticlePic(Integer articleId,MultipartFile file) throws IOException {
        return ebcArticleService.addArticlePic(articleId,file);
    }

    //todo 后台获取文章模块 分权限 平台管理员和企业管理员
    @ApiOperation(value = "后台-获取文章", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/getArticleList")
    public JsonResult getArticleList(@RequestBody ReqVo reqVo){
        System.out.println(reqVo.getCompanyId());
        int current=1;
        int size=100;
        return ebcArticleService.getArticleList(current,size,reqVo.getCompanyId());
    }

    //修改文章
    @ApiOperation(value = "修改文章, 文章模块开关(开发中..)", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/updateArticle")
    public JsonResult updateArticle(@RequestParam(value = "picFile") MultipartFile picFile,
                                    @RequestParam(value = "typeId") Integer typeId,
                                    @RequestParam(value = "articleId") Integer articleId,
                                    @RequestParam(value = "title") String title,
                                    @RequestParam(value = "viewNum") Integer viewNum,
                                    @RequestParam(value = "content") String content) throws IOException {
        ArticleRespVo respVo = new ArticleRespVo();
        respVo.setId(articleId).setContent(content).setPicFile(picFile).setTypeId(typeId).setTitle(title).setViewNum(viewNum);
        return ebcArticleService.updateArticle(respVo);
    }

    @ApiOperation(value = "删除文章", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @PostMapping("/deleteArticle")
    public JsonResult updateArticle(@RequestBody ReqVo reqVo){
        return ebcArticleService.deleteArticle(reqVo.getArticleId());
    }

    @ApiOperation(value = "获取文章分类", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @GetMapping("/getArticleType")
    public JsonResult getArticleTypeList(){
        //getArticleType
        return ebcArticleService.getArticleType();
    }
    @ApiOperation(value = "添加文章分类", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH
            +"添加文章参数: typeName、createId ")
    @PostMapping("/addArticleType")
    public JsonResult addArticleType(@RequestBody AddArticleTypeReqVo articleTypeReqVo){
        //getArticleType
        return ebcArticleService.addArticleType(articleTypeReqVo);
    }
    @ApiOperation(value = "编辑文章分类", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH
            +"编辑文章参数: typeId、typeName ")
    @PostMapping("/editArticleType")
    public JsonResult editArticleType(@RequestBody AddArticleTypeReqVo articleTypeReqVo){
        //getArticleType
        return ebcArticleService.editArticleType(articleTypeReqVo);
    }
    @ApiOperation(value = "删除文章分类", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH
            +"删除文章参数: typeId ")
    @PostMapping("/delArticleType")
    public JsonResult delArticleType(@RequestBody AddArticleTypeReqVo articleTypeReqVo){
        //getArticleType
        return ebcArticleService.deleteArticleType(articleTypeReqVo);
    }
}
